<template>
	<view>
		<view class="login-card">
			<u-input
			    placeholder="请输入邮箱"
			    border="surround"
			    v-model="email"
			  ></u-input>
			<u-input placeholder="后置插槽" v-model="code">
				<template slot="suffix">
					<u-code ref="uCode" @change="codeChange" seconds="20"></u-code>
					<u-button @tap="getCode" :text="tips" type="success" size="mini"></u-button>
				</template>
			</u-input>
			<u-button text="登录" shape="circle" type="primary" @click="login"></u-button>
		</view>
		
	</view>
</template>

<script>
	import {
		login,
		login_code
	} from '@/api/user.js'
	export default {
		data() {
			return {
				tips: '',
				email:'xingchenxuup@qq.com',
				code: '',
			}
		},
		methods: {
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					login_code({
						email: this.email
					}).then(res => {
						console.log(res)
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						uni.$u.toast('验证码已发送');
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					})
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
			end() {
				uni.$u.toast('倒计时结束');
			},
			start() {
				uni.$u.toast('倒计时开始');
			},
			login(){
				login({email:this.email,code:this.code}).then(data=>{
					console.log(data)
					this.$store.dispatch("user/saveToken",data)
					uni.$u.route({
						url: '/pages/index/index'
					})
				})
			}
		}
	}
</script>

<style scoped>
.login-card{
	width: 80%;
	margin-top: ;
	margin: 300upx auto;
}
</style>